#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
知识搜索问题诊断测试

本测试用于诊断knowledge_search功能返回空结果的问题。
通过分析ChromaDB的不同集合和文档处理状态来找出根本原因。
"""

import sys
import os
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

from src.config import load_config
from src.core.context_orchestrator import ContextOrchestrator
import asyncio
import json
from datetime import datetime

async def diagnose_knowledge_search():
    """
    诊断知识搜索问题
    """
    print("开始诊断知识搜索问题...")
    
    # 加载配置
    config = load_config()
    
    # 初始化上下文编排器
    orchestrator = ContextOrchestrator(config)
    await orchestrator.initialize()
    
    try:
        # 1. 检查ChromaDB集合状态
        print("\n=== 检查ChromaDB集合状态 ===")
        chromadb_client = orchestrator.db_manager.chromadb_client
        
        # 获取所有集合
        collections = chromadb_client.client.list_collections()
        print(f"发现 {len(collections)} 个集合:")
        
        for collection in collections:
            count = collection.count()
            print(f"  - {collection.name}: {count} 个文档")
            
            # 如果集合有文档，显示一些样本
            if count > 0:
                try:
                    # 获取前3个文档
                    results = collection.get(limit=3)
                    print(f"    样本文档ID: {results['ids'][:3] if results['ids'] else '无'}")
                    if results['metadatas']:
                        print(f"    样本元数据: {results['metadatas'][0] if results['metadatas'][0] else '无'}")
                except Exception as e:
                    print(f"    获取样本失败: {e}")
        
        # 2. 测试直接在documents集合中搜索
        print("\n=== 测试直接在documents集合中搜索 ===")
        try:
            documents_collection = chromadb_client.client.get_collection("documents")
            
            # 搜索Python相关内容
            python_results = documents_collection.query(
                query_texts=["Python编程语言"],
                n_results=3
            )
            print(f"Python搜索结果: {len(python_results['ids'][0]) if python_results['ids'] else 0} 个")
            if python_results['documents'] and python_results['documents'][0]:
                print(f"  第一个结果: {python_results['documents'][0][0][:100]}...")
            
            # 搜索Vue相关内容
            vue_results = documents_collection.query(
                query_texts=["Vue.js框架"],
                n_results=3
            )
            print(f"Vue搜索结果: {len(vue_results['ids'][0]) if vue_results['ids'] else 0} 个")
            if vue_results['documents'] and vue_results['documents'][0]:
                print(f"  第一个结果: {vue_results['documents'][0][0][:100]}...")
                
        except Exception as e:
            print(f"直接搜索失败: {e}")
        
        # 3. 检查knowledge_processor的搜索逻辑
        print("\n=== 检查knowledge_processor搜索逻辑 ===")
        knowledge_processor = orchestrator.knowledge_processor
        
        # 检查搜索配置
        print(f"相似度阈值: {knowledge_processor.similarity_threshold}")
        print(f"最大文档数: {knowledge_processor.max_documents_per_user}")
        
        # 4. 测试knowledge_processor的搜索方法
        print("\n=== 测试knowledge_processor搜索方法 ===")
        try:
            # 直接调用knowledge_processor的搜索方法
            python_search = await knowledge_processor.search_documents(
                query="Python编程语言",
                user_id="default_user",
                limit=3
            )
            print(f"knowledge_processor Python搜索: {len(python_search)} 个结果")
            
            vue_search = await knowledge_processor.search_documents(
                query="Vue.js框架",
                user_id="default_user",
                limit=3
            )
            print(f"knowledge_processor Vue搜索: {len(vue_search)} 个结果")
            
        except Exception as e:
            print(f"knowledge_processor搜索失败: {e}")
        
        # 5. 检查MongoDB中的文档状态
        print("\n=== 检查MongoDB中的文档状态 ===")
        mongodb_client = orchestrator.db_manager.mongodb_client
        
        # 查询最近的文档
        documents_collection = mongodb_client.get_collection("documents")
        recent_docs_cursor = documents_collection.find(
            {"user_id": "default_user"}
        ).sort("created_at", -1).limit(5)
        recent_docs = await recent_docs_cursor.to_list(length=5)
        
        print(f"找到 {len(recent_docs)} 个最近文档:")
        for doc in recent_docs:
            print(f"  - ID: {doc.get('_id')}")
            print(f"    标题: {doc.get('title', '无标题')}")
            print(f"    状态: {doc.get('status', '未知')}")
            print(f"    创建时间: {doc.get('created_at')}")
            if doc.get('error_message'):
                print(f"    错误信息: {doc.get('error_message')}")
            print()
        
        # 6. 生成诊断报告
        diagnosis_report = {
            "timestamp": datetime.now().isoformat(),
            "chromadb_collections": {
                col.name: col.count() for col in collections
            },
            "direct_search_results": {
                "python": len(python_results['ids'][0]) if 'python_results' in locals() and python_results['ids'] else 0,
                "vue": len(vue_results['ids'][0]) if 'vue_results' in locals() and vue_results['ids'] else 0
            },
            "knowledge_processor_results": {
                "python": len(python_search) if 'python_search' in locals() else 0,
                "vue": len(vue_search) if 'vue_search' in locals() else 0
            },
            "recent_documents_count": len(recent_docs),
            "failed_documents": [doc for doc in recent_docs if doc.get('status') == 'failed']
        }
        
        # 保存诊断报告
        with open("knowledge_search_diagnosis.json", "w", encoding="utf-8") as f:
            json.dump(diagnosis_report, f, ensure_ascii=False, indent=2, default=str)
        
        print("\n=== 诊断完成 ===")
        print("诊断报告已保存到 knowledge_search_diagnosis.json")
        
        return diagnosis_report
        
    finally:
        await orchestrator.shutdown()

if __name__ == "__main__":
    asyncio.run(diagnose_knowledge_search())